package indi.juzhuohan.chatroom.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import indi.juzhuohan.chatroom.entity.ChatStatus;
import indi.juzhuohan.chatroom.entity.TransferInfo;
import indi.juzhuohan.chatroom.io.IOStream;

/**
 * 登录界面
 * 
 * @author 鞠卓瀚
 */
public class Loginframe extends JFrame {
//	设置长，宽，背景的值
	private static final long serialVersionUID = 3522127875092787797L;
	private static final Integer FRAME_WIDTH = 430;
	private static final Integer FRAME_HEIGHT = 320;
	private static final String picPath = "G:\\java\\chatroom_socket\\src\\indi\\juzhuohan\\chatroom"
			+ "\\client\\background.jpg";

	public Loginframe() {
//		窗口
		this.setTitle("登录JJ聊天");
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		setLocation((width - FRAME_WIDTH) / 2, (height - FRAME_HEIGHT) / 2);
//		背景
		ImageIcon imageIcon = new ImageIcon(picPath);
		JLabel lblBackground = new JLabel(imageIcon);
		lblBackground.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
		lblBackground.setLayout(null);
		this.add(lblBackground);
//		账号
		JLabel lblUid = new JLabel("账号: ");
		lblUid.setBounds(90, 130, 120, 30);
		lblUid.setFont(new Font("黑体", 0, 16));
		lblUid.setForeground(Color.BLACK);
		lblBackground.add(lblUid);
		JTextField textUid = new JTextField();
		textUid.setBounds(140, 130, 180, 30);
		this.add(textUid);
//		密码
		JLabel lblPsw = new JLabel("密码: ");
		lblPsw.setBounds(90, 170, 120, 30);
		lblPsw.setFont(new Font("黑体", 0, 16));
		lblPsw.setForeground(Color.BLACK);
		lblBackground.add(lblPsw);
		JPasswordField textPsw = new JPasswordField();
		textPsw.setBounds(140, 170, 180, 30);
		this.add(textPsw);
//		登录按钮
		JButton enter = new JButton("-登录-");
		enter.setFont(new Font("黑体", 0, 16));
		enter.setBounds(95, 220, 220, 35);
//		点击登录
		enter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
//				获取账号密码
				String userName = textUid.getText();
				String password = textPsw.getText();
//				封装
				TransferInfo tif = new TransferInfo();
				tif.setUserName(userName);
				tif.setPassword(password);
				tif.setStatusEnum(ChatStatus.LOGIN);
//				链接
				connectionServer(tif);
			}
		});
		lblBackground.add(enter);
//		清空布局管理
		setLayout(null);
		setVisible(true);
	}

	/**
	 * 链接服务
	 * 
	 * @param tfi
	 */
	public void connectionServer(TransferInfo tfi) {
		try {
			Socket socket = new Socket("192.168.110.193", 8888);
			IOStream.writeMessage(socket, tfi);
			ClientHandler clientHandler = new ClientHandler(socket, this);
			clientHandler.start();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "服务器未开启", "登陆失败", JOptionPane.WARNING_MESSAGE);
		}
	}

	public static void main(String[] args) {
		new Loginframe();
	}

}
